package com.fubang.fubangexam.error;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.fubang.fubangexam.R;
import com.fubang.fubangexam.base.ExamBaseActivity;
import com.fubang.fubangexam.learning.QuestionAdapter;
import com.fubang.fubangexam.learning.QuestionFragment;
import com.fubang.fubangexam.module.QuestionModule;
import com.fubang.fubangexam.utils.ExamToastUtil;
import com.fubang.fubangexam.utils.ObjectCastUtil;

import java.util.ArrayList;
import java.util.List;

import static com.fubang.fubangexam.R.id.tv_my_page;


/**
 * 错题界面
 * <p>
 * Created by Jerry on 2017/5/7.
 */

public class ErrorActivityExam extends ExamBaseActivity {
    private ViewPager vpQuestion;
    private List<QuestionModule> questionList;
    private List<Fragment> questionFragmentList = new ArrayList<>();

    private TextView tvPage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        vpQuestion.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                showPageNum();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        // 获取问题列表
        questionList = ObjectCastUtil.cast(getIntent().getSerializableExtra("question_list"));
        if (questionList == null) {
            questionList = new ArrayList<>();
        }

        if (questionList.size() == 0) {
            hidePageNum();
            initNoQuestionFragment();
        } else {
            showPageNum();
            initQuestionFragment();
        }

        QuestionAdapter adapter = new QuestionAdapter(getSupportFragmentManager(), questionFragmentList);
        vpQuestion.setAdapter(adapter);
    }

    /**
     * 生成没有问题时页面
     */
    private void initNoQuestionFragment() {
        // TODO
        ExamToastUtil.showToast("没有错题哦", Toast.LENGTH_SHORT);
    }

    /**
     * 由问题数据集生成问题页面
     */
    private void initQuestionFragment() {
        for (int i = 0; i < questionList.size(); i++) {
            QuestionFragment singleQuestionFragment = new QuestionFragment();
            Bundle bundle = new Bundle();
            questionList.get(i).isShowCorrect = true;
            bundle.putSerializable("question", questionList.get(i));
            singleQuestionFragment.setArguments(bundle);
            questionFragmentList.add(singleQuestionFragment);
        }
    }

    private void initView() {
        vpQuestion = (ViewPager) findViewById(R.id.vp_my_question);
        tvPage = (TextView) findViewById(tv_my_page);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_question;
    }

    @Override
    protected boolean isHaveActionBarBack() {
        return true;
    }

    @Override
    protected String getActionBarTitle() {
        return "我的错题";
    }

    /**
     * 显示页数
     */
    private void showPageNum() {
        if (tvPage.getVisibility() == View.GONE) {
            tvPage.setVisibility(View.VISIBLE);
        }
        tvPage.setText((vpQuestion.getCurrentItem() + 1) + "/" + questionList.size());
    }

    private void hidePageNum() {
        if (tvPage.getVisibility() == View.VISIBLE) {
            tvPage.setVisibility(View.GONE);
        }
    }
}
